前幾篇提過頁面細部設定,對某一頁調 <head>、改 transition等細部調整。
有些調整,突然 PM 希望套用到整個網站,該怎麼辦?
而前文介紹都會提及 全域 和 單頁 的差別,有哪些設定可調呢?
這篇主要介紹各設定如何使用:
一開始不理解,通常不敢動,先來看 Nuxt 預設值
nuxt.config.js 預設值module.exports = {
  mode: 'universal',
  /*
  ** Headers of the page
  */
  head: {
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' }
    ],
    link: [
      { rel: 'stylesheet', href: 'https://fonts.googleapis.com/css?family=Roboto' }
    ]
  },
  /*
  ** Customize the progress-bar color
  */
  loading: { color: '#fff' },
  /*
  ** Global CSS
  */
  css: [],
  /*
  ** Plugins to load before mounting the App
  */
  plugins: [
  ],
  /*
  ** Nuxt.js modules
  */
  modules: [
  ],
  /*
  ** Build configuration
  */
  build: {
    /*
    ** You can extend webpack config here
    */
    extend(config, ctx) {
    }
  }
}
介紹其中常用的部分
Nuxt 除了編譯靜態頁面,另外提供兩種模式
'universal': 同構架構(Isomorphic),有SSR+CSR(包含 client-side navigation)'spa': 僅有 CSR (包含 client-side navigation)預設值
mode: 'universal'
Nuxt 全站套用的 <head> 設定,像 <title>、<meta>、<link>等 meta tag
參數同參考 vue-meta,舉幾個例
<title>網站標題 <- 定義標題句子,會填入 title 值</title>
head: {
  title: '網站標題',
  titleTemplate: '%s <- 定義標題句子,會填入 title 值',
  // titleTemplate: (titleChunk) => {
  //   /* function return 也可以 */
  //   return titleChunk ? `${titleChunk} - Site Title` : 'Site Title';
  // },
}
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="og:title" property="og:title" content="Test title - My page">
head: {
  meta: [
    { charset: 'utf-8' },
    { name: 'viewport', content: 'width=device-width, initial-scale=1' },
    {
      'property': 'og:title',
      'content': 'Test title',
      'template': chunk => `${chunk} - My page`, //or as string template: '%s - My page',
      'vmid': 'og:title'
    }
  ]
}
<link rel="stylesheet" href="/css/index.css">
<link rel="favicon" href="favicon.ico">
head: {
  link: [
    { rel: 'stylesheet', href: '/css/index.css' },
    { rel: 'favicon', href: 'favicon.ico' }
  ]
}
hid比較特別,vue-meta互蓋時,會用這唯一id判斷meta tag排列順序與覆蓋與否
head: {
  meta: [
    // hid is used as unique identifier. Do not use `vmid` for it as it will not work
    { hid: 'description', name: 'description', content: 'Meta description' }
  ]
}	
預設值
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto">
head: {
  meta: [
    { charset: 'utf-8' },
    { name: 'viewport', content: 'width=device-width, initial-scale=1' }
  ],
  link: [
    { rel: 'stylesheet', href: 'https://fonts.googleapis.com/css?family=Roboto' }
  ]
},
Nuxt 全站套用的進度條設定,這複雜一點,有三種用法
前文 12. Nuxt 客製頁面效果 - 讀取(Loading) 提過
預設進度條元件的樣式
讀取效果對應哪個元件
效果觸發時機手動控制
loading: false
設 false 後,Nuxt 不會幫你觸發讀取效果
控制權全然交給開發者,在頁面元件呼叫 開始(start)、結束(finish)才會觸發
export default {
  mounted () {
    this.$nextTick(() => {
      this.$nuxt.$loading.start()
      setTimeout(() => this.$nuxt.$loading.finish(), 500)
    })
  }
 }
預設值
白色進度條
loading: { color: '#fff' }
Nuxt 全域樣式,自動將陣列內 css 一一載入
若有裝 sass-loader,直接引用 scss/sass 也行
通常用來載入 讀取/轉場效果、首屏 所需樣式
之前 07. Nuxt.js 頁面怎麼切會更好?以電商登入頁為例 用過
  css: [
    '@/assets/sass/all.sass'
  ]
預設值
空的,僅載 Nuxt page、layout、document 寫的 style 樣式
  css: []
Nuxt Plugin 載入設定,列舉需要的 /plugin/*.js,並可設定執行環境
  plugins: ['~/plugins/my-plugin.js']
  /* 相當於此種寫法的縮寫 */
  /*
  plugins: [
    { src: '~/plugins/my-plugin.js', ssr: true }
  ]
  */
預設值
空的,不載入 plugin
  plugins: []
詳細使用與說明可參考 Nuxt Plugin 如何讓 Axios 更好用